<template>
  <div class="action-distribute">
     <Selector class="select-box" :path="fetchPath" @selectChange="selectChange" placeholder="请选择分配规则"></Selector>
     <Button type="info" icon="share" :loading="loading" @click="actionTrigger">{{actionModel.name}}</Button>
  </div>
</template>

<script>
import base from "../actionBase";
import Selector from "@/components/selector";
import $ from "@/common";
export default {
  name: "action_caseDistribute",
  extends: base,
  data() {
    return {
      rule_code: null,
      loading: false
    };
  },
  computed: {
    rangeset() {
      var rangeset = this.actionModel.param.select_rangeset || {};
      if (rangeset && typeof rangeset === "string") {
        try {
          rangeset = JSON.parse(rangeset);
        } catch (ex) {
          rangeset = {
            code: rangeset
          };
        }
      }
      return rangeset;
    },
    fetchPath() {
      if (this.rangeset) {
        if (this.rangeset.path) {
          return this.rangeset.path;
        } else if (this.rangeset.dataset || this.rangeset.code) {
          return `/service/{app}/dataset/data/${this.rangeset.dataset ||
            this.rangeset.code}`;
        }
      }
    }
  },
  methods: {
    actionTrigger() {
      return new Promise((resolve, reject) => {
        if (!this.rule_code) {
          this.$Modal.warning({
            title: "操作提示",
            content: "请先选择分配规则"
          });
          reject();
        } else {
          this.$Modal.confirm({
            title: "确认操作",
            content: this.configText(),
            onOk: () =>
              this.actionSubmit()
                .then(res => {
                  resolve(res);
                })
                .catch(res => {
                  reject(res);
                }),
            onCancel() {
              resolve();
            }
          });
        }
      });
    },
    configText() {
      var count = this.$owner.totalCount;
      var text = "";
      var rows = this.$owner.getSelectedIds();
      if (rows && rows.length) {
        count = rows.length;
        text = "确认分配当前选中的" + count + "条案件";
      } else {
        if (count) {
          text = "确认分配当前过滤的" + count + "条案件";
        } else {
          text = "确认分配当前过滤的案件";
        }
      }
      return text;
    },
    actionSubmit() {
      var data = {
        allot_rule_code: this.rule_code
      };
      var rows = this.$owner.getSelectedIds();
      if (!rows || !rows.length) {
        data.param = {};
        if (!this.$owner.filterKey || !$.trim(this.$owner.filterKey).length) {
          $.setObj(data.param, this.$owner.filterObj);
        } else {
          data.param.filter = this.$owner.filterKey;
        }
      } else {
        data.rows = rows;
      }
      this.loading = true;
      return $.link(this.actionModel.path, data)
        .then(res => {
          this.actionSuccess();
          this.$owner.update();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    selectChange(v) {
      this.rule_code = v.code;
    }
  },
  created() {
    if (!this.$owner.listData && this.$owner.$parent.listData) {
      this.$owner = this.$owner.$parent;
    }
  },
  components: {
    Selector
  }
};
</script>

<style lang="less">
.action-distribute {
  margin-right: 8px;
  display: inline-block;
  .select-box {
    display: inline-block;
  }
  .ivu-select-selection {
    border-radius: 4px 0 0 4px;
  }
  .ivu-btn {
    margin-left: -5px;
    border-radius: 0 4px 4px 0;
  }
}
</style>
